//
//  YuanliaoTBVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2025/2/6.
//  Copyright © 2025 zwb. All rights reserved.
//

class YuanliaoTBVC:ZyaTenderBaseForm {
    var itemDCFee :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
//            .assistImgStr("tenderBJ")
            .fourDemiInput(true)
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
        let v = KSFormHItem(title: "运费",appearance:appe )
        
        return v
    }()
   
    var itemSumFee :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
//            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
        let v = KSFormHItem(title: "到厂价",appearance:appe )
        v.disabled = true
        return v
    }()
    
    // 60天账期价
    var item45Price: KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
            .threeDemiInput(true)
        let v = KSFormHItem(title: "60天账期价*",appearance:appe )
        return v
    }()
    
    override func addDaochang() {
//        if TenderAppear.shared.projectType == .awClient {
            itemjg.setTempTitle("出厂价")
            itemjg.setMustInput(true)
            itemjg_input.setTempTitle("出厂价")
            itemjg_input.setMustInput(true)
            footbg.add(itemDCFee).add(itemSumFee).add(item45Price)
            item45Price.setMustInput(true)
            self.itemDCFee.disabled = (self.one.zIfArrival.unwrap == "0")
            if self.one.zIfArrival.unwrap == "0" {
                self.one.zArrivalFee = "0"
            } else {
                self.itemDCFee.setMustInput(true)
            }
//        }
    }
    override func configAction() {
        super.configAction()
        itemDCFee.textValueChangeClo = { str in
            self.one.zArrivalFee = str
            self.configDataSource()
        }
      
        item45Price.textValueChangeClo = { str in
            self.one.zAccountTermPrice = str
            self.configDataSource()
        }
    }
    override func configDataSource() {
        super.configDataSource()
        
     
        self.countSumPrice()
        
        itemDCFee.setVal(self.one.zArrivalFee)
        itemDCFee.appearance.tailDesc = "元/\(self.one.zUnitNm.unwrap)"
        itemDCFee.updateTailDesc()
        
        
        itemSumFee.setVal(self.one.zAllPrice)
        itemSumFee.appearance.tailDesc = "元/\(self.one.zUnitNm.unwrap)"
        itemSumFee.updateTailDesc()
        
//        if TenderAppear.shared.projectType == .awClient {
            item45Price.setVal(one.zAccountTermPrice)
//        }
    }
    override func  getAlertTipStr() -> String {
        "本次会消耗一次出价机会，您的出价价格是\(self.one.zAllPrice.unwrap)元，请再次核对您的出价价格，是否立即参与出价？"
    }
    override func setOriPrice() {
        var startPrice = 0.0
        if doubleTran(self.one.zAllPrice) > 0 {
            startPrice = doubleTran(self.one.zAllPrice)
        }
       
        origTbPrice = startPrice
        KSLog("设置了price\(origTbPrice)")
    }
    override func judgePrice() -> Bool {
        var judgeTemp = self.one.zAllPrice
        
        if excuteType == .add {
            judgeTemp = self.one.zPrice
            
        }
             if self.one.zType.unwrap == "0" {  // 卖标
                
                if  doubleTran(judgeTemp) >= self.origTbPrice {
                    return true
                } else {
                    KSToast("出价价格不能低于参考价格或上次出价价格")
                    return false
                }
            } else {  // 买标
                
                if doubleTran(judgeTemp) <= self.origTbPrice {
                    return true
                } else {
                    KSToast("出价价格不能高于参考价格或上次出价价格")
                    return false
                }
            }
        
      
    }
    func countSumPrice() {
        let sumP = doubleTran(self.one.zPrice) + doubleTran(self.one.zArrivalFee)
        self.one.zAllPrice = sumP.cleanZero
       
    }
    override func noNullPass() -> Bool {
        
        let vali = Validation()
        vali.required(value: one.zTbCompany, msg: "出价单位不为空")
        vali.required(value: one.zName, msg: "出价人不为空")
        vali.required(value: one.zTel, msg: "联系电话不为空")
        vali.required(value: one.zPrice, msg: "出价价格不为空")
//        if TenderAppear.shared.projectType == .awClient {
            
            let startPrice = getStartPrice()
            vali.expression(startPrice > 0, msg: "出厂价不能为空")

            vali.required(value: one.zArrivalFee, msg: "运费不能为空")
            
            vali.required(value: one.zAccountTermPrice, msg: "60天账期价不为空")
//        }
        
        guard vali.validate() else {
            return false
        }
        return true
    }
}
// 原料出价控制
//  
